Ext.require([
    'Ext.data.*',
    'Ext.grid.*'
]);

Ext.onReady(function(){

	Ext.regModel('Item', {
		fields: [
			{name: 'idItem', type: 'int'},
			{name: 'categoriesId', type: 'string'},
			{name: 'categoriesName', type: 'string'},
			{name: 'name',  type: 'string'},
			{name: 'description',  type: 'string'},
			{name: 'latitude',  type: 'float'},
			{name: 'longitude',  type: 'float'},
			{name: 'street',  type: 'string'},
			{name: 'city',  type: 'string'},
			{name: 'state',  type: 'string'},
			{name: 'country',  type: 'string'},
			{name: 'zipcode',  type: 'string'},
			{name: 'phone',  type: 'string'},
			{name: 'cellphone',  type: 'string'},
			{name: 'email',  type: 'string'},
			{name: 'skype',  type: 'string'},
			{name: 'url',  type: 'string'}
		]
	});

	function getExtraParams(){
		var s = document.location.search.split('?');
		var a = {};
		if(s.length > 1){
			a = Ext.urlDecode(s[1]);
		}
		var b = {
				memberEmail: Ext.util.Cookies.get('memberEmail'),
				memberPassword: Ext.util.Cookies.get('memberPassword')

		};

		return Ext.merge(a,b);
		
	};

    // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        model: 'Item',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: '../../cattolica-web/item/list',
            reader: {
                type: 'json',
                idProperty: 'idItem',
				root: 'items'
            },
			extraParams: getExtraParams(),
        }
    });

    // create the grid
    var grid = new Ext.grid.GridPanel({
        store: store,
        headers: [
			{text: 'Id', flex: 1, dataIndex: 'idItem', type: 'int', sortable: true},
			{text: 'Categoria', dataIndex: 'categoriesName', type: 'string', sortable: true},
			{text: 'Nome', dataIndex: 'name',  type: 'string', sortable: true},
			{text: 'Via', dataIndex: 'street',  type: 'string', sortable: true},
			{text: 'Telefono', dataIndex: 'phone',  type: 'string', sortable: true},
			{text: 'Email', dataIndex: 'email',  type: 'string', sortable: true},

			{
                xtype: 'actionheader',
                width: 50,
                items: [
					
					{
						icon   : '../shared/icons/fam/book.png',  // Use a URL in the icon config
						tooltip: 'Edit Item',
						handler: function(grid, rowIndex, colIndex) {
							var rec = store.getAt(rowIndex);
							document.location = 'edit.html?idItem=' + rec.get('idItem');
						}
					}, 
					{
						icon   : '../shared/icons/fam/image_add.png',  // Use a URL in the icon config
						tooltip: 'View Photos',
						handler: function(grid, rowIndex, colIndex) {
							var rec = store.getAt(rowIndex);
							document.location = 'photo/list.html?idItem=' + rec.get('idItem');
						}
					},					
					{
						icon   : '../shared/icons/fam/delete.gif',  // Use a URL in the icon config
						tooltip: 'Eliminare',
						handler: function(grid, rowIndex, colIndex) {
							var rec = store.getAt(rowIndex);
							var idItem = rec.get('idItem');
							Ext.Ajax.request({
								url: '../../cattolica-web/item/remove',
								params: Ext.merge(getExtraParams(), {idItem: idItem}),
								success: function(response){
									store.load();
								},
								failure: function(response) {
									Ext.Msg.alert('Failed', response.statusText);
								}
							});
						}
					}

                ]
            }

        ],
        width: 800,
        height: 200,
		iconCls: 'icon-grid',

        // inline toolbars
        tbar:[{
            text:'Alta',
            tooltip:'Alta',
            iconCls: 'icon-add',
			handler : function() {
				document.location = 'add.html';
			}
        },
		{
            text:'Alta Categoria',
            tooltip:'Alta Categoria',
            iconCls: 'icon-category',
			handler : function() {
				document.location = '../category/list.html';
			}
        }			
		]
        

    });

	grid.render('div_main');

});